package com.iot.framework.rabbitmq.service;

import com.alibaba.fastjson2.JSONObject;
import com.iot.common.enums.DeviceParamType;
import com.iot.common.utils.DateUtils;
import com.iot.common.utils.bean.BeanUtils;
import com.iot.system.domain.DeviceDetails;
import com.iot.system.domain.DeviceEnergy;
import com.iot.system.domain.DeviceParameterRecord;
import com.iot.system.domain.DeviceRelation;
import com.iot.system.service.IDeviceDetailsService;
import com.iot.system.service.IDeviceEnergyService;
import com.iot.system.service.IDeviceRelationService;
import com.iot.system.vo.DeviceParamVo;
import com.iot.system.vo.DevicePowerVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;


@Component
@Slf4j
public class DevicePowerService {

    @Resource
    private IDeviceDetailsService iDeviceDetailsService;
    @Resource
    private IDeviceEnergyService iDeviceEnergyService;
    @Resource
    private IDeviceRelationService iDeviceRelationService;


    public void devicePower(String message) {
        JSONObject jsonObject = JSONObject.parseObject(message);
        log.info("DeviceStateService_devicePower_设备能耗转换完成:{}",jsonObject);
        DevicePowerVo devicePowerVo = jsonObject.toJavaObject(DevicePowerVo.class);
        //获取设备详情
        DeviceDetails deviceDetails = iDeviceDetailsService.selectDeviceDetailsByToken(devicePowerVo.getCredentials());
        if(Objects.isNull(deviceDetails)|| Objects.isNull(devicePowerVo.getWatt())){
            return;
        }
        //判断是否子设备
        DeviceDetails mainDevice = new DeviceDetails();
        if (Objects.nonNull(devicePowerVo.getSubDevice())&& devicePowerVo.getSubDevice() == 1) {
            DeviceRelation deviceRelation = iDeviceRelationService.selectDeviceDetailsBySubDeviceId(deviceDetails.getId());
            if (Objects.nonNull(deviceRelation)){
                mainDevice.setId(deviceRelation.getMainDevice());
            }else {
                return;
            }
        }
        boolean b = Objects.equals(devicePowerVo.getSubDevice(),0);
        boolean aNull = Objects.isNull(devicePowerVo.getSubDevice());
        if (b|| aNull){
            mainDevice.setId(deviceDetails.getId());
        }

        DeviceEnergy deviceEnergy = iDeviceEnergyService.getTheLastOne(mainDevice.getId());
        if (Objects.nonNull(deviceEnergy)&&Objects.nonNull(deviceEnergy.getDeviceId())){
            DeviceEnergy record = new DeviceEnergy();
            record.setDeviceId(mainDevice.getId());
            record.setCreateTime(new Date());
            boolean sameDay = DateUtils.isSameDay(deviceEnergy.getCreateTime(), new Date());
            if (sameDay) {
                //如果不是昨天的是今天的 需要减去昨天的值
                DeviceEnergy energy = iDeviceEnergyService.getYesterdayLastOne(mainDevice.getId(),DateUtils.getDateByTimesTamp(new Date()));
                if (Objects.nonNull(energy))
                record.setDeviceEnergy(devicePowerVo.getWatt()-energy.getEnergyTotal());
                record.setEnergyTotal(devicePowerVo.getWatt());
            }else {
                record.setDeviceEnergy(devicePowerVo.getWatt()-deviceEnergy.getEnergyTotal());
                record.setEnergyTotal(devicePowerVo.getWatt());
            }
            iDeviceEnergyService.insertDeviceEnergy(record);
        }
        if (Objects.isNull(deviceEnergy)){
            DeviceEnergy record = new DeviceEnergy();
            record.setDeviceId(mainDevice.getId());
            record.setCreateTime(new Date());
            record.setDeviceEnergy(devicePowerVo.getWatt());
            iDeviceEnergyService.insertDeviceEnergy(record);
        }
    }
}
